home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / RLE_SPR.ZIP / SPRCONV.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-03  |  4.9 KB  |  210 lines

  1. // I N C L U D E /////////////////////////////////////////////////////////////
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <conio.h>
  6. #include <string.h>
  7. #include <wgt5.h>
  8.  
  9. #include "rle_spr.h"
  10.  
  11. // D E F I N E S   ///////////////////////////////////////////////////////////
  12.  
  13. #define WGTFILEFORMAT     1
  14. #define RLEFILEFORMAT     2
  15.  
  16. // G L O B A L S ////////////////////////////////////////////////////////////
  17.  
  18. char copyright[]="Sprite File Converter Copyright 1997 Aleksei Sujurov";
  19.  
  20. color pal[256];
  21. block sprites[2001];
  22.  
  23. char* infile;
  24. char* outfile;
  25.  
  26. char string[256];
  27.  
  28. char  sstr[18];
  29. short sver;
  30.  
  31. // P R O T O T Y P E S //////////////////////////////////////////////////////
  32.  
  33. void FatalError(char* Error, char* Routine, short Line);
  34. int  FileExist (char* filename);
  35. char DetectSprFormat (void);
  36. void Timer_Ctr (void);
  37. void WGT_To_RLE (void);
  38. void RLE_To_WGT (void);
  39.  
  40. // F U N C T I O N S /////////////////////////////////////////////////////////
  41.  
  42. void main (int argc, char *argv[])
  43. {
  44.     short ver;
  45.     short ch;
  46.  
  47.     fprintf (stderr, "\nWGT sprite file to/from RLE sprite file converter 1.0\n");
  48.     fprintf (stderr, "Copyright 1997 Aleksei Sujurov\n\n");
  49.  
  50.     if( argc <= 2 )
  51.     {
  52.       fprintf (stderr, "Syntax: SPRCONV infile outfile\n");
  53.       fprintf (stderr, "\tinfile\t\tWGT or RLE sprite file, which should be converted\n");
  54.       fprintf (stderr, "\toutfile\t\tName of a target file\n");
  55.       exit (-1);
  56.     }
  57.  
  58.     if ((strlen (argv[1]) >12) || (strlen (argv[2]) >12))
  59.     {
  60.        sprintf (string, "Name of a file too long !!!");
  61.        FatalError (string, "main", __LINE__);
  62.     }
  63.  
  64.     infile=strdup (argv[1]);
  65.     outfile=strdup (argv[2]);
  66.  
  67.  
  68.     if (!FileExist (infile))
  69.     {
  70.        sprintf (string, "File \"%s\" not exist !\n", infile);
  71.        FatalError (string, "main", __LINE__);
  72.     }
  73.  
  74.     if (FileExist (outfile))
  75.     {
  76.         fprintf (stderr, "File \"%s\" exist! Overwrite? (Y/N) >", outfile);
  77.         ch=getch ();
  78.  
  79.         if (ch!='Y' && ch!='y')
  80.         {
  81.            puts ("NO");
  82.            exit (-1);
  83.         }
  84.         else
  85.             puts ("YES");
  86.  
  87.     }
  88.  
  89.     ver=DetectSprFormat ();
  90.  
  91.     switch (ver)
  92.     {
  93.       case WGTFILEFORMAT:
  94.            WGT_To_RLE ();
  95.            break;
  96.  
  97.       case RLEFILEFORMAT:
  98.            RLE_To_WGT();
  99.            break;
  100.  
  101.       default:
  102.            sprintf (string, "\"%s\" is not sprite file ", infile);
  103.            FatalError (string, "main", __LINE__);
  104.     }
  105.  
  106.     wstoptimer();
  107.     wdonetimer();
  108.  
  109. }
  110.  
  111. //////////////////////////////////////////////////////////////////////////////
  112.  
  113. char DetectSprFormat (void)
  114. {
  115.     FILE* fp;
  116.     short i;
  117.  
  118.     fp=fopen (infile, "rb");
  119.  
  120.     fread (&sver, 2, 1, fp);
  121.     fread (sstr, 17, 1, fp);
  122.  
  123.     fclose (fp);
  124.  
  125.     if ((i=strnicmp (sstr, " RLE Sprite File ", 17)==0 && sver == 1))
  126.        return RLEFILEFORMAT;
  127.     else
  128.          if ((i=strnicmp (sstr, " Sprite File ", 13)==0 && sver == 4))
  129.             return WGTFILEFORMAT;
  130.  
  131.     return 0;
  132.  
  133. }
  134.  
  135. //////////////////////////////////////////////////////////////////////////////
  136.  
  137. void WGT_To_RLE (void)
  138. {
  139.  
  140.      fprintf (stderr, "\nConverting \"%s\" to \"%s\" :\n", infile, outfile);
  141.      winittimer();
  142.      wstarttimer(Timer_Ctr, TICKS(60));
  143.  
  144.      if (wloadsprites (pal, infile, sprites, 0, 2000)!=0)
  145.      {
  146.         sprintf (string, "Can't load Sprite file. (Probably shortage of memory)");
  147.         FatalError (string, "WGT_To_RLE", __LINE__);
  148.      }
  149.  
  150.      if (wsavesprites_rle (pal, outfile, sprites, 0, 2000)!=0)
  151.      {
  152.         remove (outfile);
  153.         sprintf (string, "Can't save Sprite file. (Probably disk full)");
  154.         FatalError (string, "WGT_To_RLE", __LINE__);
  155.      }
  156.  
  157. }
  158.  
  159. //////////////////////////////////////////////////////////////////////////////
  160.  
  161. void RLE_To_WGT (void)
  162. {
  163.  
  164.      fprintf (stderr, "\nConverting \"%s\" to \"%s\" :\n", infile, outfile);
  165.      winittimer();
  166.      wstarttimer(Timer_Ctr, TICKS(60));
  167.  
  168.      if (wloadsprites_rle (pal, infile, sprites, 0, 2000)!=0)
  169.      {
  170.         sprintf (string, "Can't load Sprite file. (Probably shortage of memory)");
  171.         FatalError (string, "RLE_To_WGT", __LINE__);
  172.      }
  173.      if (wsavesprites (pal, outfile, sprites, 0, 2000)!=0)
  174.      {
  175.         remove (outfile);
  176.         sprintf (string, "Can't save Sprite file. (Probably disk full)");
  177.         FatalError (string, "RLE_To_WGT", __LINE__);
  178.      }
  179.  
  180. }
  181.  
  182. //////////////////////////////////////////////////////////////////////////////
  183.  
  184. int FileExist (char* filename)
  185. {
  186.  
  187.    FILE* fp;
  188.  
  189.    fp=fopen (filename, "r");
  190.    fclose (fp);
  191.  
  192.    return fp==NULL ? 0 : 1;
  193.  
  194. }
  195.  
  196. void FatalError(char* Error, char* Routine, short Line)
  197. {
  198.  
  199.         wstoptimer();
  200.         wdonetimer();
  201.         fprintf(stderr, "\nERROR in %s, line %d! :\n%s", Routine, Line, Error);
  202.         exit(-1);
  203. }
  204.  
  205. void Timer_Ctr (void)
  206. {
  207.  
  208.      cputs (".");
  209.  
  210. }